//
//  PrefsViewController.m
//  FuelLog
//
//  Created by Pure Mass Communications on 10/18/08.
//  Copyright 2008 . All rights reserved.
//
//  Placed under GPL v2 with the kind permission of Pure Mass Communications
//
//  Modified to make compile under SDK 3.1 by Olaf Greck in May 2010
//
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#import "PrefsViewController.h"
#import "FuelLogAppDelegate.h"
#import "Pref.h"

@implementation PrefsViewController
@synthesize scrollView, doneButton;
// @synthesize infoButton, versionLabel; 
@synthesize mpgButton, ukButton, kplButton, lpkButton, lp10kButton, dpfButton, odometer0Button, odometer1Button, fuel2Button, fuel3Button, cost2Button, cost3Button;

/*
// override if customizing
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/
 
/*
// Implement loadView to create a view hierarchy programmatically.
- (void)loadView {
}
*/

 
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
    [super viewDidLoad]; 
	doneButton = 	[[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
																 target:self 
																 action:@selector(done:)];
	
	self.navigationItem.rightBarButtonItem = doneButton;
	
 	[self presetButtonColors];
	scrollView.contentSize = CGSizeMake(320, 560);
// 	versionLabel.text = [@"Ver. 3.4.2 Build: " stringByAppendingString:kBuildNumber];
}

 
- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	self.navigationItem.title = @"Preferences"; 
#warning move localisation form xib to here
		[self.navigationController setToolbarHidden:YES animated:YES];
	mpgButton.selected = NO;
	ukButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	odometer0Button.selected = NO;
	odometer1Button.selected = NO;
	fuel2Button.selected = NO;
	fuel3Button.selected = NO;
	cost2Button.selected = NO;
	cost3Button.selected = NO;
	 
	Pref *pref = [FuelLogAppDelegate pref];
	units = pref.units;
	switch (units) {
		case ENGLISH:
			mpgButton.selected = YES;
			break;
		case UK:
			ukButton.selected = YES;
			break;
		case METRIC:
			kplButton.selected = YES;
			break;
		case INV_METRIC:
			lpkButton.selected = YES;
			break;
		case NORWAY:
			lp10kButton.selected = YES;
		default:
			dpfButton.selected = YES;
	}
	
	distanceDigits = pref.odometerDigits;
	switch (distanceDigits) {
		case 1:
			odometer1Button.selected = YES;
			break;
		default:
			odometer0Button.selected = YES;
	}
	
	volumeDigits = pref.volumeDigits;
	switch (volumeDigits) {
		case 2:
			fuel2Button.selected = YES;
			break;
		default:
			fuel3Button.selected = YES;
	}
	
	costDigits = pref.costDigits;
	switch (costDigits) {
		case 3:
			cost3Button.selected = YES;
			break;
		default:
			cost2Button.selected = YES;
	}
}

- (void)viewDidUnload
{
//	self.infoButton = nil;
	self.mpgButton = nil;
	self.ukButton = nil;
	self.kplButton = nil;
	self.lpkButton = nil;
	self.lp10kButton = nil;
	self.dpfButton = nil;
	self.odometer0Button = nil;
	self.odometer1Button = nil;
	self.fuel2Button = nil;
	self.fuel3Button = nil;
	self.cost2Button = nil;
	self.cost3Button = nil;
}
#pragma mark -
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)dealloc {
	
	[scrollView release];
//	[infoButton release];
	[mpgButton release];
	[ukButton release];
	[kplButton release];
	[lpkButton release];
	[lp10kButton release];
	[dpfButton release];
	[odometer0Button release];
	[odometer1Button release];
	[fuel2Button release];
	[fuel3Button release];
	[cost2Button release];
	[cost3Button release];
    [super dealloc];
}

/*#pragma mark -

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {
    
	[self dismissModalViewControllerAnimated:YES];
}


- (IBAction)showInfo:(id)sender {    
	debug(@"in showInfo");
	FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
	controller.delegate = self;
	//	controller.titleView = @"Test";
	//	controller.navigationItem.title = @"Hallo";
	controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:controller animated:YES];
	
	[controller release];
}

*/
#pragma mark -

- (IBAction)done:(id)sender {
	Pref *pref = [FuelLogAppDelegate pref];
	[pref setPrefs:distanceDigits volumeDigits:volumeDigits costDigits:costDigits units:units];
	[self.navigationController popViewControllerAnimated:YES];
	//	[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)cancel:(id)sender {
	[self.navigationController popViewControllerAnimated:YES];
	//	[self dismissModalViewControllerAnimated:YES];
}

#pragma mark -

-(void)presetButtonColors {

	[self.mpgButton useSystemToggleStyle];
	[self.ukButton useSystemToggleStyle];
	[self.kplButton useSystemToggleStyle];
	[self.lpkButton useSystemToggleStyle];
	[self.lp10kButton useSystemToggleStyle];
	[self.dpfButton useSystemToggleStyle];
	
 
	[self.odometer0Button useSystemToggleStyle];
	[self.odometer1Button useSystemToggleStyle];
	[self.fuel2Button useSystemToggleStyle];
	[self.fuel3Button useSystemToggleStyle];
	[self.cost2Button useSystemToggleStyle];
	[self.cost3Button useSystemToggleStyle];
//	[self.infoButton useSystemTouchStyle];
	
}

- (IBAction)mpgClick:(id)sender {
	ukButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	mpgButton.selected = YES;
	units = ENGLISH;
}

- (IBAction)ukClick:(id)sender {
	mpgButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	ukButton.selected = YES;
	units = UK;
}

- (IBAction)kplClick:(id)sender {
	mpgButton.selected = NO;
	ukButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	kplButton.selected = YES;
	units = METRIC;
}

- (IBAction)lpkClick:(id)sender {
	mpgButton.selected = NO;
	ukButton.selected = NO;
	kplButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = NO;
	lpkButton.selected = YES;
	units = INV_METRIC;

}

- (IBAction)lp10kClick:(id)sender {
	mpgButton.selected = NO;
	ukButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	dpfButton.selected = NO;
	lp10kButton.selected = YES;
	units = NORWAY;

}

- (IBAction)dpfClick:(id)sender {
	mpgButton.selected = NO;
	ukButton.selected = NO;
	kplButton.selected = NO;
	lpkButton.selected = NO;
	lp10kButton.selected = NO;
	dpfButton.selected = YES;
	units = GENERIC;

}

- (IBAction)odometer0Click:(id)sender {
	odometer1Button.selected = NO;
	odometer0Button.selected = YES;
	distanceDigits = 0;

}

- (IBAction)odometer1Click:(id)sender {
	odometer0Button.selected = NO;
	odometer1Button.selected = YES;
	distanceDigits = 1;

}

- (IBAction)fuel2Click:(id)sender {
	fuel3Button.selected = NO;
	fuel2Button.selected = YES;
	volumeDigits = 2;

}

- (IBAction)fuel3Click:(id)sender {
	fuel2Button.selected = NO;
	fuel3Button.selected = YES;
	volumeDigits = 3;

}

- (IBAction)cost2Click:(id)sender {
	cost3Button.selected = NO;
	cost2Button.selected = YES;
	costDigits = 2;

}

- (IBAction)cost3Click:(id)sender {
	cost2Button.selected = NO;
	cost3Button.selected = YES;
	costDigits = 3;

}

@end
